******************************
** Update KAST AND POMERANZ **
******************************
* Update values in NBER WP to the ones in a published paper at JHR.

matrix savingsrates = J(30,5,.)
local rr = 0 // row counter
local b_col  1
local se_col 2
local lo_col 3
local hi_col 4
local p_col  5
matrix colnames savingsrates = "b" "se" "lo" "hi" "p"
scalar _alpha = 0.05 // significance level


local a=0
local auth "kast14"
local months = 12 // Always use annual income as denominator 	

// Breakdown for savings part is:
//  39% active users have average 18456, sd 77,672
//  of remaining 61%, 
//		14% (of total) take up but left minimum balance, 1000;
//  	47% (of total) do not take up, hence balance = 0
// To get the mean and standard deviation for non-active users:

*[OLD] local n_treatment = 2279 // Table 2(in NBER WP)
*[OLD] local n_takeup = 1218    // Table 2 (in NBER WP)
*[OLD] local n_active = 886     // Table 2 (in NBER WP)

local n_treatment = 2279 // Table A3 (in JHR)　
local n_takeup = 1218    // Table A3 (in JHR)
local n_active = 895     // Table A3 (in JHR)
local n_nonactive  = `n_treatment' - `n_active' 
local n_notakeup   = `n_treatment' - `n_takeup' 
clear
set obs `n_nonactive'
gen x = 0
replace x = 1000 in 1/`n_notakeup'
summ x
scalar _mean_nonactive = r(mean)
scalar _var_nonactive   = r(sd)^2

*[OLD] scalar _mean_active = 18456 // Table 2 (in NBER WP)
*[OLD] scalar _var_active = 77672^2   // Table 2 (in NBER WP)

scalar _mean_active = 18269 // Table A3 (in JHR)
scalar _var_active = 77303^2   // Table A3 (in JHR)


scalar _b_savings = (`n_active'/`n_treatment')*_mean_active + ///
	((`n_takeup'-`n_active')/`n_treatment')*1000

// Total variance for ITT effect (Headrick, 2010, eqn 5.38):
#delimit ;
scalar _V_savings = 
	(
		(`n_nonactive'^2) * _var_nonactive + (`n_active'^2) * _var_active - 
		`n_nonactive'*_var_nonactive - `n_nonactive'*_var_active - 
		`n_active'*_var_nonactive - `n_active'*_var_active +
		`n_nonactive'*`n_active'*_var_nonactive + `n_nonactive'*`n_active'*_var_active +
		`n_nonactive'*`n_active'*(_mean_nonactive-_mean_active)^2 
	)/(
		(`n_nonactive' + `n_active' - 1)*(`n_nonactive' + `n_active')
	)
; /* note I tested to make sure this formula works with a toy example in 
		test_totalvar.R */
#delimit cr

*[OLD] local n_total = 3572 // Full sample treatment and control; table 3 (in NBER WP)
*[OLD] scalar _b_debt = -12931 // Table 3 effect of peer groups on debt (in NBER WP)
*[OLD] scalar _V_debt = `n_total'*(5867^2) // V = N*se^2; se from Table 3 (in NBER WP)

local n_total = 3551 // Full sample treatment and control; Table 2 (in JHR)
scalar _b_debt = -12163 // Table 2 effect of peer groups on debt (in JHR)
scalar _V_debt = `n_total'*(5803^2) // V = N*se^2; se from Table 2 (in JHR)


scalar _b_ = _b_savings - _b_debt // net savings
di _b_
scalar _V_ = _V_savings + _V_debt // - 2Cov(savings,debt)
	// assumes independence (i.e. Cov(savings,debt)=0) since we don't know 
	// variance-covariance matrix (don't have microdata)
scalar _se_ = sqrt(_V_)/sqrt(`n_total')
scalar _df_ = 307 - 3 // 307 is number of groups; 
	// 3 is for constant, post, account x post
	
scalar _denom_ = 4.27*79955 // number of household members * 
	// per capita monthly income (both from Table 1)
	
local T ""
	
local ++rr
local rownames `rownames' `auth'
foreach est in b se {
	scalar _`est'_`auth'_`T' = _`est'_`T'/(_denom_`T'*`months')
	di as text _n "``est'':" 
	di as result _`est'_`auth'_`T'
	local estimate_list "`estimate_list' _`est'_`auth'_`T'"
	matrix savingsrates[`rr',``est'_col'] = _`est'_`auth'_`T'
}
scalar _lo_`auth'_`T' = _b_`auth'_`T' - invttail(_df_,_alpha/2)*_se_`auth'_`T'
scalar _hi_`auth'_`T' = _b_`auth'_`T' + invttail(_df_,_alpha/2)*_se_`auth'_`T'
scalar _p_`auth'_`T'  = 2*ttail(_df_,abs(_b_`auth'_`T'/_se_`auth'_`T')) 
foreach est in lo hi p {
	matrix savingsrates[`rr',``est'_col'] = _`est'_`auth'_`T'
}




***************************************
** Create comparison graph (take up) **
***************************************


import delimited "rawdata/aux/savings_rates.csv",clear


keep if type=="takeup"
drop if auth == "seshan14"

gsort -b 
gen n = _n 



gen label = article + " ("+ intervention +")"
replace label = article + " (" + string(year)+ ", "+ intervention +")" if year!=.
labmask n, values(label)


* gen b2 =  0.1*b if article=="This Study (Mobile)"
* gen se2 =  0.1*se if article=="This Study (Mobile)"


* gen lo2  = b2 - 1.96*se 
* gen hi2  = b2 + 1.96*se 

replace lo  = b - 1.96*se 
replace hi  = b + 1.96*se 

describe,detail
twoway rcap  hi lo n if type=="takeup", horizontal ||  scatter  n b if type=="takeup"&auth=="tanaka", msymbol(circle) mcolor(gs1)  ||  scatter  n b if type=="takeup"&auth!="tanaka",  msymbol(diamond) mcolor(gs8)   ylabel(1(1)`r(N)',valuelabel angle(0)) ytitle("") xtitle("Effect on Take-up Rate")  xline(0) xsize(10) legend(label(1 "95% CI") label(2 "Point Estimate of This Study") label(3 "Point Estimate of Other Studies") order(2 3 1) cols(1) position(7))

graph  export "final/comparisongraph_takeup.eps",replace



***************************************
** Create comparison graph (saving) **
***************************************

import delimited "rawdata/aux/savings_rates.csv",clear


* Replace values of kast14 (NBER WP) with the values in the published version (JHR)
replace b = savingsrates[1,1] if auth == "kast14"
replace se = savingsrates[1,2] if auth == "kast14"
replace lo = savingsrates[1,3] if auth == "kast14"
replace hi = savingsrates[1,4] if auth == "kast14"
replace p = savingsrates[1,5] if auth == "kast14"

replace article = "Pomeranz and Kast" if auth == "kast14"
replace year = 2022 if auth == "kast14"
replace journal = "JHR" if auth == "kast14"

replace auth = "pomeranz22" if auth == "kast14"


* Add Galiani et al. 2022
set obs `=_N+1'
replace auth = "galiani22" in `=_N'
replace b = 10.840 / 2835 if auth == "galiani22" // Table 3 and footnote 17
replace se = 3.597 / 2835 if auth == "galiani22" // Table 3 and footnote 17
replace lo = b - 1.96*se if auth == "galiani22"
replace hi = b + 1.96*se if auth == "galiani22"

replace type = "saving" if auth == "galiani22"
replace article = "Galiani et al." if auth == "galiani22"
replace year = 2022 if auth == "galiani22"
replace journal = "JDE" if auth == "galiani22"
replace intervention = "Trust" if auth == "galiani22"
replace extra = "10 months" if auth == "galiani22"
replace country = "Peru" if auth == "galiani22"


keep if type!="takeup"|auth=="tanaka"
drop if auth=="seshan14"
drop if longer_term==1



replace b =  0.12*b if auth=="tanaka"
replace se =  0.12*se if auth=="tanaka"

replace lo  = b - 1.96*se if auth=="tanaka" 
replace hi  = b + 1.96*se if auth=="tanaka" 
gsort -b 
gen n = _n 


gen label = article + " ("+ intervention +")"
replace label = article + " (" + string(year)+ ", "+ intervention +")" if year!=.

labmask n, values(label)
describe,detail

twoway rcap  hi lo n ,  horizontal ||  scatter  n b if auth=="tanaka", msymbol(circle) mcolor(gs1)  ||  scatter  n b if auth!="tanaka",  msymbol(diamond) mcolor(gs8)   ylabel(1(1)`r(N)',valuelabel angle(0)) ytitle("") xtitle("Effect on Stock of Saving as Proportion of Annual Income")   xline(0) xsize(10) legend(label(1 "95% CI") label(2 "Point Estimate of This Study") label(3 "Point Estimate of Other Studies") order(2 3 1) cols(1) position(7))

graph  export "final/comparisongraph_saving.eps",replace

